package com.william.jdbcplus.core.util;

import com.william.jdbcplus.core.toolkit.StringPool;
import com.william.jdbcplus.core.toolkit.StringUtil;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 执行脚本方法
 */
public class JdbcExecUtil {
    /**
     * 执行SQL
     * <p>
     * 报错继续执行下一句
     * </p>
     *
     * @param sqlList    要执行的sql列表，按顺序执行
     * @param dataSource 数据源
     */
    public static void exec(List<String> sqlList, DataSource dataSource) {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            for (String sql : sqlList) {
                String[] sqlSegment = StringUtil.split(sql, StringPool.SEMICOLON);
                for (String segment : sqlSegment) {
                    String exec = segment.trim();
                    if (StringUtil.isNotBlank(exec)) {
                        try {
                            ScriptUtils.executeSqlScript(connection, new ByteArrayResource((exec + StringPool.SEMICOLON).getBytes()));
                        } catch (Exception e) {
                            continue;
                        }
                    }
                }
            }
        } catch (SQLException e) {
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException ex) {
            }
        }
    }

    /**
     * 执行SQL
     * 报错返回false, 否则返回true
     *
     * @param sql        要执行的sql
     * @param dataSource 数据源
     */
    public static boolean execOne(String sql, DataSource dataSource) {
        boolean flag = true;
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            String[] sqlSegment = StringUtil.split(sql, StringPool.SEMICOLON);
            for (String segment : sqlSegment) {
                String exec = segment.trim();
                if (StringUtil.isNotBlank(exec)) {
                    try {
                        ScriptUtils.executeSqlScript(connection, new ByteArrayResource((exec + StringPool.SEMICOLON).getBytes()));
                    } catch (Exception e) {
                        flag = false;
                    }
                }
            }
        } catch (SQLException e) {
            flag = false;
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException ex) {
            }
        }

        return flag;
    }

    /**
     * 执行SQL 中途报错则中止执行后面的脚本
     * 返回执行是否有错误
     *
     * @param sql 要执行的sql
     * @param ds  数据源
     */
    public static boolean execBreak(String sql, DataSource ds) {
        boolean flag = true;
        Connection connection = null;
        try {
            connection = ds.getConnection();
            String[] sqlSegment = StringUtil.split(sql, StringPool.SEMICOLON);
            for (String segment : sqlSegment) {
                String exec = segment.trim();
                if (StringUtil.isNotBlank(exec)) {
                    try {
                        ScriptUtils.executeSqlScript(connection, new ByteArrayResource((exec + StringPool.SEMICOLON).getBytes()));
                    } catch (Exception e) {
                        flag = false;
                        break;
                    }
                }
            }
        } catch (SQLException e) {
        } finally {
            try {
                if (null != connection) {
                    connection.close();
                }
            } catch (SQLException ex) {
            }
        }

        return flag;
    }
}
